package com.timedomain.him.remake.ui.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.timedomain.him.remake.R;
import com.timedomain.him.remake.core.ListPlayer;
import com.timedomain.him.remake.service.PlayerService;
import com.timedomain.him.remake.utils.AppSettings;
import com.timedomain.him.remake.utils.BaseActivity;
import com.timedomain.him.remake.utils.TimeUtils;

import org.jetbrains.annotations.Nullable;

import java.util.List;

public class PlayerActivity extends BaseActivity {

    public static void StartPlayer(BaseActivity ctx, List<ListPlayer.PlayEntry> entries, ListPlayer.PlayEntry current){
        if(PlayerService.player != null){
            PlayerService.StopPlayerService(ctx);
            ctx.setTimeout(new Runnable() {
                @Override
                public void run() {
                    PlayerService.StartPlayerService(ctx,new ListPlayer(ctx,entries,current));
                    ctx.startActivity(new Intent(ctx,PlayerActivity.class));
                }
            },400);
        }
        else{
            PlayerService.StartPlayerService(ctx,new ListPlayer(ctx,entries,current));
            ctx.startActivity(new Intent(ctx,PlayerActivity.class));
        }

    }

    private ImageButton btnLoopMode;
    private ToggleButton btnPlayPause;
    private TextView txtSleepTimerIndicator;
    private TextView txtPlayerTotalTime;
    private TextView txtPlayerCurrentTime;
    private SeekBar seekbarProgress;
    private TextView txtPlayerTitle;
    private com.timedomain.him.remake.views.WaveformView playerWaveform;
    private void initUI(){	 btnLoopMode = (ImageButton)findViewById(R.id.btnLoopMode);
        btnPlayPause = (ToggleButton)findViewById(R.id.btnPlayPause);
        txtSleepTimerIndicator = (TextView)findViewById(R.id.txtSleepTimerIndicator);
        txtPlayerTotalTime = (TextView)findViewById(R.id.txtPlayerTotalTime);
        txtPlayerCurrentTime = (TextView)findViewById(R.id.txtPlayerCurrentTime);
        seekbarProgress = (SeekBar)findViewById(R.id.seekbarProgress);
        txtPlayerTitle = (TextView)findViewById(R.id.txtPlayerTitle);
        playerWaveform = (com.timedomain.him.remake.views.WaveformView)findViewById(R.id.playerWaveform);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        initUI();
        btnPlayPause.setOnClickListener(this::btnPausePlayClick);
        setTimerInterval(50);
        setTimerEnabled(true);
        seekbarProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                seekbarSliding = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                seekbarSliding = false;
                if(! (PlayerService.player instanceof ListPlayer)){
                    return;
                }
                ListPlayer lp = (ListPlayer) PlayerService.player;
                lp.setPosition(seekBar.getProgress());
            }
        });
        updateLoopDisplay();
        syncData();

    }

    void updateLoopDisplay(){
        switch (AppSettings.playerLoopMode){
            case 0:
                btnLoopMode.setImageResource(R.mipmap.loop_one);
                break;
            case 1:
                btnLoopMode.setImageResource(R.mipmap.loop_all);
                break;
            case 2:
                btnLoopMode.setImageResource(R.mipmap.loop_random);
                break;
        }


    }

    @Override
    protected void onTimer() {
        syncData();
    }

    boolean seekbarSliding = false;

    void syncData(){
        if(! (PlayerService.player instanceof ListPlayer)){
            finish();
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;

        // waveform
        playerWaveform.Amplitude = lp.getPeak();
        int maxProgress = (int) lp.getDuration();
        int currentProgress = (int) lp.getPosition();
        if(seekbarProgress.getMax() != maxProgress){
            seekbarProgress.setMax(maxProgress);
        }
        if(!seekbarSliding){
            seekbarProgress.setProgress(currentProgress);
        }

        txtPlayerCurrentTime.setText(TimeUtils.msToTimeStr(currentProgress));
        txtPlayerTotalTime.setText(TimeUtils.msToTimeStr(maxProgress));

        txtPlayerTitle.setText(lp.getTitle());

        int sleepTimer = lp.getSleepTimer();
        if(sleepTimer >= 0){

            txtSleepTimerIndicator.setText(""+sleepTimer);
        }
        else{
            if(sleepTimer == -2){

                txtSleepTimerIndicator.setText("ON");
            }
            else{
                txtSleepTimerIndicator.setText("");
            }
        }
        btnPlayPause.setChecked(lp.isPlaying());

    }


    public void toggleSleepTimer(View view) {
        if(! (PlayerService.player instanceof ListPlayer)){
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;
        if(lp.getSleepTimer() == -1 || (lp.getSleepTimer() >=0 && lp.getSleepTimer() < 14)){
            lp.setSleepTimer(15);
            showToast("15分钟后停止");

        }else if(lp.getSleepTimer() >= 59 ){
            lp.setSleepTimer(-2);
            showToast("当前音频结束后停止");
        }
        else if(lp.getSleepTimer() >= 29 ){
            lp.setSleepTimer(60);
            showToast("60分钟后停止");

        }
        else if(lp.getSleepTimer() >= 14){
            lp.setSleepTimer(30);
            showToast("30分钟后停止");

        }else if(lp.getSleepTimer() == -2){
            lp.setSleepTimer(-1);
            showToast("关闭定时停止");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(! (PlayerService.player instanceof ListPlayer)){
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;
        if(!lp.isPlaying()){
            PlayerService.StopPlayerService(this);
        }
        else{
            showToast("从通知栏可以回到播放页面");
        }
    }

    public void btnPrev(View view) {
        if(! (PlayerService.player instanceof ListPlayer)){
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;
        lp.prev();
    }

    public void btnNext(View view) {
        if(! (PlayerService.player instanceof ListPlayer)){
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;
        lp.next();
    }

    public void btnToggleLoopMode(View view) {
        if(! (PlayerService.player instanceof ListPlayer)){
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;

        AppSettings.playerLoopMode++;
        if(AppSettings.playerLoopMode > 2){AppSettings.playerLoopMode = 0;}
        AppSettings.saveSettings(this);
        lp.setLoopMode(AppSettings.playerLoopMode);
        updateLoopDisplay();

        switch (AppSettings.playerLoopMode){
            case 0:
                showToast("单个循环");
                break;
            case 1:
                showToast("列表循环");
                break;
            case 2:
                showToast("随机播放");
                break;
        }
    }

    public void btnPausePlayClick(View v){
        if(! (PlayerService.player instanceof ListPlayer)){
            return;
        }
        ListPlayer lp = (ListPlayer) PlayerService.player;
        if(lp.isPlaying()){
            lp.pause();
            btnPlayPause.setChecked(false);
        }
        else{
            lp.start();
            btnPlayPause.setChecked(true);
        }

    }
}
